{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "             货号  商品名称  尺码         日期  售卖价  实收金额  销量   平台  季节\n0      YZFM1200    夹克   L 2019-09-01  208   166   2  平台A  秋季\n1      FLHR1800  短袖T恤  XL 2019-09-01   73    73   1  平台A  夏季\n2      LCHM9800  保暖内衣   M 2019-09-01  148   103   1  平台A  冬季\n3      FLHR1800  短袖T恤   M 2019-09-01   73    51   1  平台A  夏季\n4      HWLA4700  长袖T恤   M 2019-09-01   88    52   1  平台A  秋季\n...         ...   ...  ..        ...  ...   ...  ..  ...  ..\n17515  FLHR1800  短袖T恤  XL 2019-06-30   73    65   1  平台B  夏季\n17516  QTVW5600   羽绒服   M 2019-06-30  538   322   1  平台B  四季\n17517  QTVW5600   羽绒服  XL 2019-06-30  538   269   1  平台B  四季\n17518  LCHM9800  保暖内衣   M 2019-06-30  148   103   1  平台B  冬季\n17519  HWLA4700  长袖T恤   L 2019-06-30   88    70   1  平台B  秋季\n\n[17520 rows x 9 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>货号</th>\n      <th>商品名称</th>\n      <th>尺码</th>\n      <th>日期</th>\n      <th>售卖价</th>\n      <th>实收金额</th>\n      <th>销量</th>\n      <th>平台</th>\n      <th>季节</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>YZFM1200</td>\n      <td>夹克</td>\n      <td>L</td>\n      <td>2019-09-01</td>\n      <td>208</td>\n      <td>166</td>\n      <td>2</td>\n      <td>平台A</td>\n      <td>秋季</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>FLHR1800</td>\n      <td>短袖T恤</td>\n      <td>XL</td>\n      <td>2019-09-01</td>\n      <td>73</td>\n      <td>73</td>\n      <td>1</td>\n      <td>平台A</td>\n      <td>夏季</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>LCHM9800</td>\n      <td>保暖内衣</td>\n      <td>M</td>\n      <td>2019-09-01</td>\n      <td>148</td>\n      <td>103</td>\n      <td>1</td>\n      <td>平台A</td>\n      <td>冬季</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>FLHR1800</td>\n      <td>短袖T恤</td>\n      <td>M</td>\n      <td>2019-09-01</td>\n      <td>73</td>\n      <td>51</td>\n      <td>1</td>\n      <td>平台A</td>\n      <td>夏季</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>HWLA4700</td>\n      <td>长袖T恤</td>\n      <td>M</td>\n      <td>2019-09-01</td>\n      <td>88</td>\n      <td>52</td>\n      <td>1</td>\n      <td>平台A</td>\n      <td>秋季</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>17515</th>\n      <td>FLHR1800</td>\n      <td>短袖T恤</td>\n      <td>XL</td>\n      <td>2019-06-30</td>\n      <td>73</td>\n      <td>65</td>\n      <td>1</td>\n      <td>平台B</td>\n      <td>夏季</td>\n    </tr>\n    <tr>\n      <th>17516</th>\n      <td>QTVW5600</td>\n      <td>羽绒服</td>\n      <td>M</td>\n      <td>2019-06-30</td>\n      <td>538</td>\n      <td>322</td>\n      <td>1</td>\n      <td>平台B</td>\n      <td>四季</td>\n    </tr>\n    <tr>\n      <th>17517</th>\n      <td>QTVW5600</td>\n      <td>羽绒服</td>\n      <td>XL</td>\n      <td>2019-06-30</td>\n      <td>538</td>\n      <td>269</td>\n      <td>1</td>\n      <td>平台B</td>\n      <td>四季</td>\n    </tr>\n    <tr>\n      <th>17518</th>\n      <td>LCHM9800</td>\n      <td>保暖内衣</td>\n      <td>M</td>\n      <td>2019-06-30</td>\n      <td>148</td>\n      <td>103</td>\n      <td>1</td>\n      <td>平台B</td>\n      <td>冬季</td>\n    </tr>\n    <tr>\n      <th>17519</th>\n      <td>HWLA4700</td>\n      <td>长袖T恤</td>\n      <td>L</td>\n      <td>2019-06-30</td>\n      <td>88</td>\n      <td>70</td>\n      <td>1</td>\n      <td>平台B</td>\n      <td>秋季</td>\n    </tr>\n  </tbody>\n</table>\n<p>17520 rows × 9 columns</p>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_excel(\n",
    "    '2019销售总表.xlsx'\n",
    ").convert_dtypes()\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "货号              string\n商品名称            string\n尺码              string\n日期      datetime64[ns]\n售卖价              Int64\n实收金额             Int64\n销量               Int64\n平台              string\n季节              string\ndtype: object"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "140288"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['季节'].memory_usage()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "      销量\n季节      \n冬季  7259\n四季  5464\n夏季  1862\n春季  3694\n秋季  3600",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>销量</th>\n    </tr>\n    <tr>\n      <th>季节</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>冬季</th>\n      <td>7259</td>\n    </tr>\n    <tr>\n      <th>四季</th>\n      <td>5464</td>\n    </tr>\n    <tr>\n      <th>夏季</th>\n      <td>1862</td>\n    </tr>\n    <tr>\n      <th>春季</th>\n      <td>3694</td>\n    </tr>\n    <tr>\n      <th>秋季</th>\n      <td>3600</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tb = pd.pivot_table(\n",
    "    df,\n",
    "    index='季节',\n",
    "    values='销量',\n",
    "    aggfunc=sum\n",
    ")\n",
    "tb"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "      销量\n季节      \n冬季  7259\n四季  5464\n夏季  1862\n春季  3694\n秋季  3600",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>销量</th>\n    </tr>\n    <tr>\n      <th>季节</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>冬季</th>\n      <td>7259</td>\n    </tr>\n    <tr>\n      <th>四季</th>\n      <td>5464</td>\n    </tr>\n    <tr>\n      <th>夏季</th>\n      <td>1862</td>\n    </tr>\n    <tr>\n      <th>春季</th>\n      <td>3694</td>\n    </tr>\n    <tr>\n      <th>秋季</th>\n      <td>3600</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tb.sort_index()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "Index([b'\\xe5\\x86\\xac\\xe5\\xad\\xa3', b'\\xe5\\x9b\\x9b\\xe5\\xad\\xa3',\n       b'\\xe5\\xa4\\x8f\\xe5\\xad\\xa3', b'\\xe6\\x98\\xa5\\xe5\\xad\\xa3',\n       b'\\xe7\\xa7\\x8b\\xe5\\xad\\xa3'],\n      dtype='object', name='季节')"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tb.index.str.encode('utf-8')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "CategoricalDtype(categories=['春季', '夏季', '秋季', '冬季', '四季'], ordered=True)"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_type = pd.CategoricalDtype(\n",
    "    categories=['春季', '夏季', '秋季', '冬季', '四季'],\n",
    "    ordered=True # 按照我们设置的列表进行排序\n",
    ")\n",
    "my_type"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "货号              string\n商品名称            string\n尺码              string\n日期      datetime64[ns]\n售卖价              Int64\n实收金额             Int64\n销量               Int64\n平台              string\n季节            category\ndtype: object"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " # 转化为自定义的type\n",
    "df['季节'] = df['季节'].astype(my_type)\n",
    "df.dtypes"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "17848"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['季节'].memory_usage()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "      销量\n季节      \n春季  3694\n夏季  1862\n秋季  3600\n冬季  7259\n四季  5464",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>销量</th>\n    </tr>\n    <tr>\n      <th>季节</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>春季</th>\n      <td>3694</td>\n    </tr>\n    <tr>\n      <th>夏季</th>\n      <td>1862</td>\n    </tr>\n    <tr>\n      <th>秋季</th>\n      <td>3600</td>\n    </tr>\n    <tr>\n      <th>冬季</th>\n      <td>7259</td>\n    </tr>\n    <tr>\n      <th>四季</th>\n      <td>5464</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tb1 = pd.pivot_table(\n",
    "    df,\n",
    "    index='季节',\n",
    "    values='销量',\n",
    "    aggfunc=sum\n",
    ")\n",
    "tb1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "CategoricalIndex(['春季', '夏季', '秋季', '冬季', '四季'], categories=['春季', '夏季', '秋季', '冬季', '四季'], ordered=True, name='季节', dtype='category')"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tb1.index"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}